package com.xcity.game.guild.gmember;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.fastjson.JSON;

public class GuildMemberData {
	public static int DATA_VERSION = 1;
	protected int lastDonateDay = 0;
	protected Map<Integer, GuildMemberDonate> type2donate;
	public GuildMemberData(){
	}

	public GuildMemberData(Map<Integer, GuildMemberDonate> map, int lastDonateDay){
		this.lastDonateDay = lastDonateDay;
		if(map == null){
			this.type2donate = new ConcurrentHashMap<Integer, GuildMemberDonate>();
			initData();
		}else{
			this.type2donate = map;
		}
	}
	
	public GuildMemberData checkVersion(int version){
		if(version != DATA_VERSION){
			initData();
		}
		return this;
	}
	
	public void initData(){
		if(!type2donate.containsKey(GuildMemberDonate.DONATE_TYPE_FREE)){
			type2donate.put(GuildMemberDonate.DONATE_TYPE_FREE, new GuildMemberDonate(GuildMemberDonate.DONATE_TYPE_FREE, 0, 0));
		}
		if(!type2donate.containsKey(GuildMemberDonate.DONATE_TYPE_1)){
			type2donate.put(GuildMemberDonate.DONATE_TYPE_1, new GuildMemberDonate(GuildMemberDonate.DONATE_TYPE_1, 0, 0));
		}
		if(!type2donate.containsKey(GuildMemberDonate.DONATE_TYPE_2)){
			type2donate.put(GuildMemberDonate.DONATE_TYPE_2, new GuildMemberDonate(GuildMemberDonate.DONATE_TYPE_2, 0, 0));
		}
	}
	
	/**
	 * 今日已捐献次数
	 * @param type 类型
	 */
	public int getDonateCountToday(int type, int today){
		GuildMemberDonate data = type2donate.get(type);
		if(data != null){
			if(lastDonateDay == today){
				return data.getLastDonateCount();
			}else{
				return 0;
			}
		}
		return -1;
	}
	
	public int addDonateCountToday(int type, int today){
		if(lastDonateDay != today){
			for(GuildMemberDonate data : type2donate.values()){
				data.reset();
			}
			lastDonateDay = today;
		}
		GuildMemberDonate data = type2donate.get(type);
		if(data != null){
			int count = data.getLastDonateCount();
			count = count + 1;
			data.setLastDonateCount(count);
			return count;
		}
		return 0;
	}
	
	public void resetDonate(){
		lastDonateDay = 0;		
	}
	
	@Override
	public String toString(){
		return DATA_VERSION + ";" + lastDonateDay + ";" + JSON.toJSONString(type2donate);
	}
}
